package com.liuweijun.win;

import cn.hutool.json.JSONUtil;
import com.liuweijun.win.data.IP.IPData;
import com.liuweijun.win.data.Iphone.IphoneData;
import com.liuweijun.win.data.Vehicle.VehicleData;
import com.liuweijun.win.dbHelper.DBHelper;
import com.liuweijun.win.util.HttpClient;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class HelloController {
    @FXML
    private Label ipLabel;
    @FXML
    private Label vehicleLabel;
    @FXML
    private Label iphoneLabel;
    @FXML
    private TextField ipText;
    @FXML
    private TextField vehicleText;
    @FXML
    private TextField iphoneText;

    @FXML
    protected void onIPButtonClick() throws SQLException {
//        welcomeText.setText("Welcome to JavaFX Application!");
        String ip=ipText.getText();
        boolean flag = false;
        Connection conn = DBHelper.getMySQLConnection("jdbc:mysql://localhost:3306/test","root","123456");
        ResultSet resultSet = DBHelper.executeQuery(conn,"select * from dizhi");
        while(resultSet.next()){
            if(flag = resultSet.getString(1).equals(ip))
                break;
        }
        if(flag){
            ipLabel.setText("从数据库中获取此ip信息:"+resultSet.getString(2));
        }else {
            String url = "https://qqlykm.cn/api/free/ip/get?key=R5arP5KQY5q5FaBRWQ8cTVqVA6&ip=" + ip;
            String data = HttpClient.Get(url);
            IPData ipData = JSONUtil.toBean(data, IPData.class);
            String msg = "从网页中获取当前ip信息" + ipData.getData().getIp();
            msg += "\n当前ip归属地：" + ipData.getData().getLocation();
            ipLabel.setText(msg);
            DBHelper.executeUpdate(conn,"insert into dizhi values('"+ip+"','"+ipData.getData().getLocation()+"');");

        }
    }
    @FXML
    protected void onVehicleButtonClick() throws SQLException {
        String vehicle=vehicleText.getText();
        boolean flag = false;
        Connection conn = DBHelper.getMySQLConnection("jdbc:mysql://localhost:3306/test","root","123456");
        ResultSet resultSet = DBHelper.executeQuery(conn,"select * from cp");
        while(resultSet. next()){
            if(flag = resultSet.getString(1).equals(vehicle))
                break;
        }
        if(flag){
            vehicleLabel.setText("从数据库中获取此车辆信息:"+resultSet.getString(2)+resultSet.getString(3));
        }else{
            String url="https://qqlykm.cn/api/plateNumber/?key=R5arP5KQY5q5FaBRWQ8cTVqVA6&word="+vehicle;
            String data= HttpClient.Get(url);
            VehicleData vehicleData= JSONUtil.toBean(data, VehicleData.class);
            String msg = "在网页中获取此车辆信息:"+vehicleData.getData().getProvince();
            msg+="  "+vehicleData.getData().getCity();
            vehicleLabel.setText(msg);
            DBHelper.executeUpdate(conn,"insert into cp values('"+vehicle+"','"+vehicleData.getData().getProvince()+"','"+vehicleData.getData().getCity()+"');");
        }
    }
    @FXML
    protected void onIphoneButtonClick() throws SQLException {
            String iphone=iphoneText.getText();
            boolean flag = false;
            Connection conn = DBHelper.getMySQLConnection("jdbc:mysql://localhost:3306/test","root","123456");
            ResultSet resultSet = DBHelper.executeQuery(conn,"select * from hm");
            while(resultSet.next()){
            if(flag = resultSet.getString(1).equals(iphone))
                break;
            }
            if(flag){
            iphoneLabel.setText("从数据库中获取此号码信息:"+"\n"+resultSet.getString(2)+"\n"+resultSet.getString(3)+"\n"+resultSet.getString(4)
            +"\n"+resultSet.getString(5)+"\n"+resultSet.getString(6));
            }else {
                String url = "https://qqlykm.cn/api/free/phone/get?key=R5arP5KQY5q5FaBRWQ8cTVqVA6&phone=" + iphone;
                String data = HttpClient.Get(url);
                IphoneData iphoneData = JSONUtil.toBean(data, IphoneData.class);
                String msg = "\n当前手机号码：" + iphone;
                msg += "\n当前电话号码归属地:" + iphoneData.getData().getProvince();
                msg += "\n" + iphoneData.getData().getCity();
                msg += "\n当前区域编码：" + iphoneData.getData().getArea_code();
                msg += "\n当前邮政编码：" + iphoneData.getData().getPost_code();
                msg += "\n当前运营商：" + iphoneData.getData().getIsp();
                iphoneLabel.setText(msg);
                DBHelper.executeUpdate(conn,"insert into hm values ('"+iphone+"','"+iphoneData.getData().getProvince()+"','"+iphoneData.getData().getCity()+"','"+iphoneData.getData().getArea_code()+"','"+iphoneData.getData().getPost_code()+"','"+iphoneData.getData().getIsp()+"');");
            }
        }
    }

